<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="位置" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入位置"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开始时间">
        <el-date-picker
          v-model="queryParams.beginTime"
          type="date"
           value-format="yyyy-MM-dd"
          placeholder="选择日期时间"
        >
        </el-date-picker>
      </el-form-item>
       <el-form-item label="结束时间">
        <el-date-picker
          v-model="queryParams.endTime"
         type="date"
           value-format="yyyy-MM-dd"
          placeholder="选择日期时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-card  v-for="(item, index) in dataList" :key="index" class="box-card">
          <div slot="header" class="clearfix">
            <span>{{ item.address+'  主要污染物：'+item.wuran +'  污染级别：'+item.jibie}}</span>
           
          </div>
          <el-row>
             <el-col :span="8"> {{ "PM2.5: " + item.pm25 }}</el-col>
            <el-col :span="8">{{ "PM10: " + item.pm10 }}</el-col>
            <el-col :span="8">{{ "TSP: " + item.tsp }}</el-col>
          </el-row>
        <el-row>
             <el-col :span="8"> {{ "噪声: " + item.zaosheng }}</el-col>
            <el-col :span="8"> {{ "温度: " + item.wendu }}</el-col>
            <el-col :span="8">{{ "湿度: " + item.shidu }}</el-col>
          </el-row>
           <el-row>
             <el-col :span="8">{{ "风速: " + item.fengsu }}</el-col>
            <el-col :span="8">{{ "风向: " + item.fengxiang }}</el-col>
            <el-col :span="8">{{ "气压: " + item.qiya }}</el-col>
          </el-row>

      </el-card>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
// 这个函数 网上  随处可见，我也是应用了别人的。
function formatDate(date, fmt) {
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(
      RegExp.$1,
      (date.getFullYear() + "").substr(4 - RegExp.$1.length)
    );
  }
  let o = {
    "M+": date.getMonth() + 1,
    "d+": date.getDate(),
    "h+": date.getHours(),
    "m+": date.getMinutes(),
    "s+": date.getSeconds(),
  };
  for (let k in o) {
    if (new RegExp(`(${k})`).test(fmt)) {
      let str = o[k] + "";
      fmt = fmt.replace(
        RegExp.$1,
        RegExp.$1.length === 1 ? str : padLeftZero(str)
      );
    }
  }
  return fmt;
}

function padLeftZero(str) {
  return ("00" + str).substr(str.length);
}
import {
  listData,
  getData,
  delData,
  addData,
  updateData,
  exportData,
} from "@/api/system/data";
import { listAddress } from "@/api/system/address";
export default {
  name: "Data",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      pickerOptions: {
        shortcuts: [
          {
            text: "最近半天",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 12);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一天",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三天",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 3);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 监测数据表格数据
      dataList: [],
      addressOptions: [],
      // 弹出层标题
      title: "",
      dateRange: [],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        addressId: undefined,
        address: undefined,
        lat: undefined,
        lng: undefined,
        wuran: undefined,
        jibie: undefined,
        aqi: undefined,
        pm25: undefined,
        pm10: undefined,
        tsp: undefined,
        zaosheng: undefined,
        wendu: undefined,
        shidu: undefined,
        fengsu: undefined,
        fengxiang: undefined,
        qiya: undefined,
        createTime: undefined,
        beginTime: undefined,
        endTime: undefined,
        orderByColumn:'create_time',
        isAsc:'DESC'
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
    listAddress().then((res) => {
      this.addressOptions = res.rows;
    });
  },
  methods: {
    /** 查询监测数据列表 */
    getList() {
      this.loading = true;

      listData(this.queryParams).then(
        (response) => {
          this.dataList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        dataId: undefined,
        address: undefined,
        lat: undefined,
        lng: undefined,
        wuran: undefined,
        jibie: undefined,
        aqi: undefined,
        pm25: undefined,
        pm10: undefined,
        tsp: undefined,
        zaosheng: undefined,
        wendu: undefined,
        shidu: undefined,
        fengsu: undefined,
        fengxiang: undefined,
        qiya: undefined,
        createBy: undefined,
        createTime: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.dataId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加监测数据";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const dataId = row.dataId || this.ids;
      getData(dataId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改监测数据";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.form.addressId;
      for (let i in this.addressOptions) {
        if (this.addressOptions[i].addressId == this.form.addressId) {
          this.form.address = this.addressOptions[i].addName;
          this.form.lng = this.addressOptions[i].lng;
          this.form.lat = this.addressOptions[i].lat;
        }
      }
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.dataId != undefined) {
            updateData(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addData(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const dataIds = row.dataId || this.ids;
      this.$confirm(
        '是否确认删除监测数据编号为"' + dataIds + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delData(dataIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function () {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有监测数据数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportData(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        })
        .catch(function () {});
    },
  },
};
</script>